package factory.simple.order;

import factory.simple.pizza.CheesePizza;
import factory.simple.pizza.GreekPizza;
import factory.simple.pizza.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Author chenyeqing
 * @Date 2021/7/7 22:59
 */
public class OrderPizza {
    // 构造器
    /*public OrderPizza() throws IOException {
        Pizza pizza = null;
        // 订购的披萨类型
        String orderType;

        do {
            orderType = getType();
            if ("greek".equals(orderType)) {
                pizza = new GreekPizza();
                pizza.setName("希腊披萨");
            } else if ("cheese".equals(orderType)) {
                pizza = new CheesePizza();
                pizza.setName("奶酪披萨");
            } else {
                break;
            }
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();
        } while (true);
    }*/

    public OrderPizza(SimpleFactory simpleFactory) throws IOException {
        setSimpleFactory(simpleFactory);
    }

    SimpleFactory simpleFactory = null;

    public void setSimpleFactory(SimpleFactory simpleFactory) throws IOException {
        // 用户输入
        String type = "";

        this.simpleFactory = simpleFactory;
        Pizza pizza = null;
        // 订购的披萨类型
        String orderType;
        do {
            orderType = getType();
            pizza = simpleFactory.createPizza(orderType);
            if (pizza != null) {
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            } else {
                System.out.println("没有这个Pizza");
                break;
            }
        } while (true);

    }

    /**
     * 写一个方法 可以获取用户希望订购的披萨种类
     */
    private String getType() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("input pizza type:");
        String s = bufferedReader.readLine();
        return s;
    }

}
